from appium import webdriver

from APP_UI_TEST.contacts_podemo.page.main_page import MainPage


class APP():
    def start(self):
        # 启动APP
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['deviceName'] = 'LEIDIAN Emulator'
        #  通过命令获取package/activity : adb logcat ActivityManger:I | findstr "cmp"
        desired_caps['appPackage'] = 'com.tencent.wework'
        desired_caps['appActivity'] = '.launch.LaunchSplashActivity'
        desired_caps['noReset'] = 'true'  # 防止 清缓存数据
        desired_caps['autoGrantPermissions'] = 'true'
        desired_caps['skipDeviceInitialization'] = 'true'  # 跳过设备初始化 ,跳过settings.apk的安装与设置
        desired_caps['skipServerInstallation'] = 'true'  # 跳过uiautomato2 服务安装
        desired_caps['settings[waitForIdleTimeout]'] = 0  # 等待页面处于idle状态 ，默认10s

        self.driver = webdriver.Remote("127.0.0.1:4723/wd/hub", desired_caps)
        # 隐式等待，每一次查找元素的时候，动态的查找
        self.driver.implicitly_wait(5)
        return self

    def back(self,num=1):
        """APP后退num次"""
        for i in range(num):
            self.driver.back()

    def stop(self):
        self.driver.quit()

    def go_to_main_page(self):
        # 进入到通讯录首页
        return MainPage(self.driver)